package com.csit.dao.impl;

import org.hibernate.Criteria;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import com.csit.bean.Pager;
import com.csit.dao.DataDictionaryDao;
import com.csit.entity.DataDictionary;
import com.csit.util.StringUtil;

@Repository
public class DataDictionaryDaoImpl extends BaseDaoImpl<DataDictionary, Integer> implements DataDictionaryDao {

	public Pager queryDataDictionary(Pager pager, DataDictionary dataDictionary) {
		Assert.notNull(dataDictionary, "DataDictionary is required");
		Criteria criteria = getSession().createCriteria(DataDictionary.class);
		if(StringUtil.isNotNull(dataDictionary.getDataDictionaryType())){
			criteria.add(Restrictions.eq("dataDictionaryType", dataDictionary.getDataDictionaryType()));
		}
		if(StringUtil.isNotNull(dataDictionary.getDataDictionaryName())){
			criteria.add(Restrictions.like("dataDictionaryName", dataDictionary.getDataDictionaryName(), MatchMode.ANYWHERE));
		}
		Long totalCount = (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();

		criteria.setProjection(null);
		criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
		criteria.setFirstResult(pager.getBeginCount());
		criteria.setMaxResults(pager.getPageSize());
		criteria.addOrder(Order.asc("dataDictionaryType")).addOrder(Order.asc("dataDictionaryName"));
		pager.setTotalCount(totalCount);
		pager.setList(criteria.list());
		return pager;
	}

}
